package com.redside.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

/**
 * @author PCHAPMAN
 * 
 */
@Entity
@Table(name = "complaint")
public class Complaint extends BaseObject implements java.io.Serializable {

    private static final long serialVersionUID = -7136211436158211155L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "complaint_id")
    private Long id;

    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name = "initial_complaint_id", nullable = false)
    private InitialComplaint initialComplaint;

    @ManyToOne
    @JoinColumn(name = "user_id")
    private User countyUser;

    @ManyToOne
    @JoinColumn(name = "business_id")
    private Business business;

    @Column(name = "status_id", nullable = false)
    private Long complaintStatusId;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "complaint_date")
    private Date complaintDate;

    @Lob
    @Column(name = "complaint_text")
    private String complaintText;

    @Column(name = "violation_id", nullable = false)
    private Long violationTypeId;
    
    @Column(name = "nature_of_complaint")
    private Long natureOfComplaintId;

    @Column(name = "actionable", nullable = false)
    private boolean actionable;

    @Column(name = "first_valid", nullable = false)
    private boolean firstValidComplaint;

    @Column(name = "nonactionable_id")
    private Long nonActionableId;

    // private Set<ComplaintAction> complaintActions = new HashSet<ComplaintAction>(0);
    // private Remediation remediations;
    // private SiteVisit siteVisits;
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "complaint_id")
    private List<Correspondence> correspondences = new ArrayList<Correspondence>();
    
    // private Citation citations;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "complaint_id")
    private Set<Note> notes = new HashSet<Note>(0);

    @Column(name = "new_note", length = 250)
    private String newNote;

    public Complaint() {
    }

    /**
     * @param initialComplaint
     * @param complaintStatusLkp
     * @param complaintDate
     * @param complaintText
     * @param actionable
     * @param firstValid
     */
    public Complaint(InitialComplaint initialComplaint,
            Long complaintStatusId, 
            Date complaintDate,
            Long violationTypeId,
            Long natureOfComplaintId,
            String complaintText, 
            boolean actionable,
            boolean firstValid) {
        this.initialComplaint = initialComplaint;
        this.complaintStatusId = complaintStatusId;
        this.complaintDate = complaintDate;
        this.violationTypeId = violationTypeId;
        this.natureOfComplaintId=natureOfComplaintId;
        this.complaintText = complaintText;
        this.actionable = actionable;
        this.firstValidComplaint = firstValid;
    }
    
    /**
     * @param complaintStatusLkp
     * @param complaintDate
     * @param complaintText
     * @param actionable
     * @param firstValid
     */
    public Complaint(Long complaintStatusId, 
            Date complaintDate,
            Long violationTypeId,
            String complaintText, 
            boolean actionable,
            boolean firstValid) {
        this.complaintStatusId = complaintStatusId;
        this.complaintDate = complaintDate;
        this.violationTypeId = violationTypeId;
        this.complaintText = complaintText;
        this.actionable = actionable;
        this.firstValidComplaint = firstValid;
    }   

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @param id
     *            the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @return the initialComplaint
     */
    public InitialComplaint getInitialComplaint() {
        return initialComplaint;
    }

    /**
     * @param initialComplaint
     *            the initialComplaint to set
     */
    public void setInitialComplaint(InitialComplaint initialComplaint) {
        this.initialComplaint = initialComplaint;
    }

    /**
     * @return the countyUser
     */
    public User getCountyUser() {
        return countyUser;
    }

    /**
     * @param countyUser
     *            the countyUser to set
     */
    public void setCountyUser(User countyUser) {
        this.countyUser = countyUser;
    }

    /**
     * @return the business
     */
    public Business getBusiness() {
        return business;
    }

    /**
     * @param business
     *            the business to set
     */
    public void setBusiness(Business business) {
        this.business = business;
    }

    /**
	 * @return the complaintStatusId
	 */
	public Long getComplaintStatusId() {
		return complaintStatusId;
	}

	/**
	 * @param complaintStatusId the complaintStatusId to set
	 */
	public void setComplaintStatusId(Long complaintStatusId) {
		this.complaintStatusId = complaintStatusId;
	}

	/**
     * @return the complaintDate
     */
    public Date getComplaintDate() {
        return complaintDate;
    }

    /**
     * @param complaintDate
     *            the complaintDate to set
     */
    public void setComplaintDate(Date complaintDate) {
        this.complaintDate = complaintDate;
    }

    /**
     * @return the complaintText
     */
    public String getComplaintText() {
        return complaintText;
    }

    /**
     * @param complaintText
     *            the complaintText to set
     */
    public void setComplaintText(String complaintText) {
        this.complaintText = complaintText;
    }

    /**
	 * @return the violationTypeId
	 */
	public Long getViolationTypeId() {
		return violationTypeId;
	}

	/**
	 * @param violationTypeId the violationTypeId to set
	 */
	public void setViolationTypeId(Long violationTypeId) {
		this.violationTypeId = violationTypeId;
	}

	/**
	 * @return the natureOfComplaintId
	 */
	public Long getNatureOfComplaintId() {
		return natureOfComplaintId;
	}

	/**
	 * @param natureOfComplaintId the natureOfComplaintId to set
	 */
	public void setNatureOfComplaintId(Long natureOfComplaintId) {
		this.natureOfComplaintId = natureOfComplaintId;
	}

	/**
     * @return the actionable
     */
    public boolean isActionable() {
        return actionable;
    }

    /**
     * @param actionable
     *            the actionable to set
     */
    public void setActionable(boolean actionable) {
        this.actionable = actionable;
    }

    /**
     * @return the firstValidComplaint
     */
    public boolean isFirstValidComplaint() {
        return firstValidComplaint;
    }

    /**
     * @param firstValidComplaint
     *            the firstValidComplaint to set
     */
    public void setFirstValidComplaint(boolean firstValidComplaint) {
        this.firstValidComplaint = firstValidComplaint;
    }

	/**
	 * @return the nonActionableId
	 */
	public Long getNonActionableId() {
		return nonActionableId;
	}

	/**
	 * @param nonActionableId the nonActionableId to set
	 */
	public void setNonActionableId(Long nonActionableId) {
		this.nonActionableId = nonActionableId;
	}

	/**
	 * @return the correspondences
	 */
	public List<Correspondence> getCorrespondences() {
		return correspondences;
	}

	/**
	 * @param correspondences the correspondences to set
	 */
	public void setCorrespondences(List<Correspondence> correspondences) {
		this.correspondences = correspondences;
	}

	/**
     * @return the notes
     */
    public Set<Note> getNotes() {
        return notes;
    }

    /**
     * @param notes
     *            the notes to set
     */
    public void setNotes(Set<Note> notes) {
        this.notes = notes;
    }

    /**
     * @return the newNote
     */
    public String getNewNote() {
        return newNote;
    }

    /**
     * @param newNote
     *            the newNote to set
     */
    public void setNewNote(String newNote) {
        this.newNote = newNote;
    }

	/**
	 * @see java.lang.Object#equals(Object)
	 */
	public boolean equals(Object object) {
		if (!(object instanceof Complaint)) {
			return false;
		}
		Complaint rhs = (Complaint) object;
		return new EqualsBuilder().append(
				this.countyUser, rhs.countyUser).append(this.newNote,
				rhs.newNote).append(this.complaintText, rhs.complaintText)
				.append(this.initialComplaint, rhs.initialComplaint).append(
						this.business, rhs.business).append(this.id, rhs.id)
				.append(this.natureOfComplaintId, rhs.natureOfComplaintId)
				.append(this.complaintStatusId, rhs.complaintStatusId).append(
						this.correspondences, rhs.correspondences).append(
						this.nonActionableId, rhs.nonActionableId).append(
						this.actionable, rhs.actionable).append(
						this.complaintDate, rhs.complaintDate).append(
						this.violationTypeId, rhs.violationTypeId).append(
						this.notes, rhs.notes).append(this.firstValidComplaint,
						rhs.firstValidComplaint).isEquals();
	}

	/**
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return new HashCodeBuilder(-940400379, 1462136567).append(this.countyUser)
			.append(this.newNote)
			.append(this.complaintText).append(this.initialComplaint)
			.append(this.business).append(this.id)
			.append(this.natureOfComplaintId)
			.append(this.complaintStatusId).append(this.correspondences)
			.append(this.nonActionableId)
			.append(this.actionable)
			.append(this.complaintDate)
			.append(this.violationTypeId)
			.append(this.notes)
			.append(this.firstValidComplaint)
			.toHashCode();
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return new ToStringBuilder(this).append("actionable", this.actionable)
				.append("complaintStatusId", this.complaintStatusId).append(
						"complaintText", this.complaintText).append(
						"nonActionableId", this.nonActionableId).append(
						"newNote", this.newNote).append("initialComplaint",
						this.initialComplaint)
				.append("business", this.business).append("countyUser",
						this.countyUser).append("violationTypeId",
						this.violationTypeId).append("correspondences",
						this.correspondences).append("complaintDate",
						this.complaintDate).append("firstValidComplaint",
						this.firstValidComplaint).append("notes", this.notes)
				.append("natureOfComplaintId", this.natureOfComplaintId)
				.append("id", this.id).toString();
	}

}